package org.tis.tools.abf.module.om.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.om.controller.request.OmBatchDeleteRequest;
import org.tis.tools.abf.module.om.entity.OmGroupApp;
import org.tis.tools.abf.module.om.service.IOmGroupAppService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * omGroupApp的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/omGroupApp")
@Slf4j
public class OmGroupAppController extends BaseController<OmGroupApp>  {

    @Autowired
    private IOmGroupAppService omGroupAppService;

    @ApiOperation(value = "新增工作组拥有应用",notes = "传入OmGroupApp类作为参数")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "OmGroupApp",name = "传入的OmGroupApp类",required = true) OmGroupApp omGroupApp) {
        omGroupAppService.insert(omGroupApp);
        return ResultVO.success("新增成功！");
    }


    @ApiOperation(value = "修改工作组拥有应用",notes = "传入OmGroupApp类作为参数")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "OmGroupApp",name = "传入的OmGroupApp类",required = true) OmGroupApp omGroupApp) {
        OmGroupApp omGroupAppQue = omGroupAppService.selectById(omGroupApp.getGuid());
        if (omGroupAppQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omGroupAppService.updateById(omGroupApp);
        return ResultVO.success("修改成功！",omGroupApp);
    }


    @ApiOperation(value = "删除工作组拥有应用",notes = "传入工作组应用GUID作为参数")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "工作组应用GUID", required = true) String id) {
        OmGroupApp omGroupApp = omGroupAppService.selectById(id);
        if (omGroupApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omGroupAppService.deleteById(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "批量删除工作组应用",notes = "传入工作组GUID(guid)和应用GUID组(deleteList)类作为参数")
    @OperateLog(type = OperateType.DELETE,desc ="批量删除工作组应用")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "OmBatchDeleteRequest",name = "传入参数OmBatchDeleteRequest", required = true) OmBatchDeleteRequest omBatchDeleteRequest) {
        omGroupAppService.batchDelete(omBatchDeleteRequest);
        return ResultVO.success("删除成功！");
    }



    @ApiOperation(value = "根据GUID查询工作组拥有应用信息",notes = "传入工作组应用GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "工作组应用GUID", required = true) String id) {
        OmGroupApp omGroupApp = omGroupAppService.selectById(id);
        if (omGroupApp == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omGroupApp);
    }


    @ApiOperation(value = "分页查询工作组拥有应用信息",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<OmGroupApp> page) {
        return  ResultVO.success("查询成功", omGroupAppService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }
    
}

